﻿using LS.Nc6.Web.Entry.ViewModel.UI;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;

namespace LS.Nc6.Web.Entry.ViewComponents
{
    public class NewListViewComponent : ViewComponent
    {
        private readonly ICmsChannelService _cmsChannelService;
        private readonly ICmsContentService _contentService;
        public NewListViewComponent(ICmsChannelService cmsChannelService , ICmsContentService contentService)
        {
            _cmsChannelService= cmsChannelService;
            _contentService = contentService;
        }
        public  async Task<IViewComponentResult> InvokeAsync(long id,int pageIndx)
        {
            CmsChannelListVm cmschannel = new CmsChannelListVm();
            List<CmsContentVm> itemlist = new List<CmsContentVm>();
            cmschannel.PageSize = 10;
            cmschannel.PageIndx = pageIndx;
            var result = await _contentService.PageDynamicList(a=>a.ChannelId== id, pageIndx, cmschannel.PageSize, true, a=>a.AddTime, a => new {
                id = a.Id,
                title = a.Title,
                top = a.Top,
                recommend = a.Recommend,
                hot = a.Hot,
                color = a.Color,
                channelId = a.ChannelId,
                createDate = a.AddTime,
                userName = a.UserName,
                checkedState = a.Checked,
                summary =a.Summary,
                subTitle =a.SubTitle,
                addTime =a.AddTime,
            });
            foreach (var item in result.Item1)
            {
                itemlist.Add(new CmsContentVm {
                     Id=item.id,
                     Title=item.title,
                     Summary =item.summary,
                     SubTitle=item.subTitle,
                     AddTime=Convert.ToDateTime(item.addTime).ToString("yyyy-MM-dd"),
                });
            }
            cmschannel.Id= id;
            cmschannel.TotalRow = result.Item2;
            cmschannel.ContentList = itemlist;
            return View(cmschannel);
        }
    }
}
